package com.lock;

import java.util.concurrent.TimeUnit;

/**
 * @Author: sn
 * @Desc something
 * @Date: 2022/4/27
 */
public class TestWait5 {

    static  Object object = new Object();//锁对象
    static boolean isWoman = false; // 是否有女人

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {//jack
            synchronized (object){
                while (!isWoman){//判断是否有女人
                    //log.debug("没有女人 我去等待老板安排 先休息，安排好之后叫醒我");
                    System.out.println(String.format("{%s}-没有女人 我去等待老板安排 先休息，安排好之后叫醒我",Thread.currentThread().getName()));
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //log.debug("开始工作啪啪啪啪");
                System.out.println(String.format("{%s}-开始工作啪啪啪啪",Thread.currentThread().getName()));
            }
        }, "jack").start();


        TimeUnit.SECONDS.sleep(1);

        System.out.println("1s之后主线程获取锁");
        System.out.println("因为jack wait释放了锁，主线程能够获取到");
        System.out.println("-----------关键看打印顺序----------------");
        synchronized (object) {

            for (int i = 0; i < 5; i++) {
                new Thread(() -> {
                    synchronized (object) {
                        System.out.println(String.format("{%S}-那些默默无闻的程序员coding",Thread.currentThread().getName()));
                    }
                }, "t" + i).start();
                TimeUnit.MILLISECONDS.sleep(1);
            }

            isWoman=true;
            object.notifyAll();
        }

    }
}
